package com.castle.fortress.admin.temp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.temp.entity.TempTreeTableEntity;
import com.castle.fortress.admin.temp.dto.TempTreeTableDto;
import com.castle.fortress.admin.temp.service.TempTreeTableService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 生成左树右表示例 控制器
 *
 * @author 
 * @since 2023-08-02
 */
@Api(tags="生成左树右表示例管理控制器")
@Controller
public class TempTreeTableController {
	@Autowired
	private TempTreeTableService tempTreeTableService;

	/**
	 * 生成左树右表示例的分页展示
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @param current 当前页
	 * @param size  每页记录数
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.QUERY)
	@ApiOperation("生成左树右表示例分页展示")
	@GetMapping("/temp/tempTreeTable/page")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:pageList")
	public RespBody<IPage<TempTreeTableDto>> pageTempTreeTable(TempTreeTableDto tempTreeTableDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
		Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<TempTreeTableDto> page = new Page(pageIndex, pageSize);
			IPage<TempTreeTableDto> pages = tempTreeTableService.pageTempTreeTable(page, tempTreeTableDto);

		return RespBody.data(pages);
	}

	/**
	 * 生成左树右表示例的列表展示
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.QUERY)
	@ApiOperation("生成左树右表示例列表展示")
	@GetMapping("/temp/tempTreeTable/list")
	@ResponseBody
	public RespBody<List<TempTreeTableDto>> listTempTreeTable(TempTreeTableDto tempTreeTableDto){
		List<TempTreeTableDto> list = tempTreeTableService.listTempTreeTable(tempTreeTableDto);
		return RespBody.data(list);
	}

	/**
	 * 生成左树右表示例保存
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.INSERT)
	@ApiOperation("生成左树右表示例保存")
	@PostMapping("/temp/tempTreeTable/save")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:save")
	public RespBody<String> saveTempTreeTable(@RequestBody TempTreeTableDto tempTreeTableDto){
		if(tempTreeTableDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
          TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
		if(tempTreeTableService.save(tempTreeTableEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例编辑
	 * @param tempTreeTableDto 生成左树右表示例实体类
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.UPDATE)
	@ApiOperation("生成左树右表示例编辑")
	@PostMapping("/temp/tempTreeTable/edit")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:edit")
	public RespBody<String> updateTempTreeTable(@RequestBody TempTreeTableDto tempTreeTableDto){
		if(tempTreeTableDto == null || tempTreeTableDto.getId() == null || tempTreeTableDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
          TempTreeTableEntity tempTreeTableEntity = ConvertUtil.transformObj(tempTreeTableDto,TempTreeTableEntity.class);
		if(tempTreeTableService.updateById(tempTreeTableEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例删除
	 * @param id
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.DELETE)
	@ApiOperation("生成左树右表示例删除")
	@PostMapping("/temp/tempTreeTable/delete")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:delete")
	public RespBody<String> deleteTempTreeTable(@RequestParam Long id){
		if(id == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(tempTreeTableService.removeById(id)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}


	/**
	 * 生成左树右表示例批量删除
	 * @param ids
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.DELETE)
	@ApiOperation("生成左树右表示例批量删除")
	@PostMapping("/temp/tempTreeTable/deleteBatch")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:deleteBatch")
	public RespBody<String> deleteTempTreeTableBatch(@RequestBody List<Long> ids){
		if(ids == null || ids.size()<1){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(tempTreeTableService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 生成左树右表示例详情
	 * @param id 生成左树右表示例id
	 * @return
	 */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.QUERY)
	@ApiOperation("生成左树右表示例详情")
	@GetMapping("/temp/tempTreeTable/info")
	@ResponseBody
	@RequiresPermissions("temp:tempTreeTable:info")
	public RespBody<TempTreeTableDto> infoTempTreeTable(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
        TempTreeTableDto tempTreeTableDto =  tempTreeTableService.getByIdExtends(id);
		return RespBody.data(tempTreeTableDto);
	}

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="生成左树右表示例",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/temp/tempTreeTable/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<TempTreeTableDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<TempTreeTableDto> list = tempTreeTableService.listTempTreeTable(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
